﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics;
using MixScript.DBService;
using MixScript.BLL;

namespace MixScript.BLL.MM
{
    public class cBarang : cBaseBLL
    {
        private const string lcTableName = "tb_barang";
        private string msg = "";
        private string sSQL = "";
        private int[] arridxgenid = new int[] {1 };
        private string[] arrgenid = new string[] {"GENMATNR" };


        public cBarang(string modulnm, string prosnm)
            : base(modulnm, prosnm)
        {
            

        }
        public void baru()
        {
        }


        public bool isExist(DataTable dt, string kdbarang, out int idx)
        {
            try
            {
                idx = 0;
                foreach (DataRow dr in dt.Rows)
                {

                    if (dr["kdbarang"].ToString() == kdbarang)
                    {

                        return true;

                    }
                    idx++;

                }
                return false;


            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public DataTable initDTPenjualan()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = Dbserver.ExecuteQuery("select trxdetail,trxid,kdbarang,'' as namabarang,qty,harga, totharga from tb_trxdetail limit 0", "tb_trxdetail");
                return dt;


            }
            catch (Exception ex)
            {

                throw;
            }
        }
        public bool deleteRow(string kodeBarang, out string msg)
        {
            try
            {
                msg = "";
                DataTable dt = new DataTable();
                // inqTableName = "tb_transstudio";
                sSQL = "select * from tb_barang where kdbarang='" + kodeBarang + "' ";
                dt = Dbserver.ExecuteQuery(sSQL, "tb_barang");
                if (dt.Rows.Count > 0)
                {
                    //  inqTableName = "vw_booking";
                    if (Dbserver.openConn())
                    {
                        dt.Rows[0].Delete();
                        Dbserver.SqlUpdate(dt, "tb_barang", out msg);
                        Dbserver.CommitAndCloseConn();
                    }
                    msg = "Delete Success";
                    return true;
                }
                else
                {
                    //    inqTableName = "vw_booking";
                    msg = "Records not Found";
                    return false;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        
        public bool addNewMaterial(out string msg1, params object[] fldvalue)
        {
            
            return AddNewRowDB(out msg1, lcTableName, arridxgenid, arrgenid,false, fldvalue); ;

        }
    
        public override bool  AddNewRowDB(out string msg1, string tablename, int[] indexgenfieldname, string[] genid, bool haveHistory, params object[] fldValue)
{
 	 return base.AddNewRowDB(out msg1, tablename, indexgenfieldname, genid, haveHistory, fldValue);
}
      

        public bool getInquiry(ref DataTable dt, string kdbarang, string namabarang, decimal harga, decimal stok, out string msg)
        {
            try
            {

                msg = "";
                sSQL = "select * from tb_barang where 1=1 ";

                if (kdbarang.Trim() != string.Empty)
                {
                    sSQL += "and kdbarang='" + kdbarang + "' ";
                }
                if (namabarang.Trim() != string.Empty)
                {
                    sSQL += "and namabarang='" + namabarang + "' ";
                }
                if (harga > 0)
                {
                    sSQL += "and harga= " + harga.ToString() + " ";
                }
                if (stok > 0)
                {
                    sSQL += "and stok = " + stok.ToString() + " ";
                }


                sSQL += "order by kdbarang";
                //dt = Dbserver.ExecuteQuery(sSQL, inqTableName);


                if (dt.Rows.Count > 0)
                {
                    msg = dt.Rows.Count.ToString() + " Record(s)";
                    return true;
                }
                else
                {
                    msg = "Records Not Found";
                    return false;
                }


            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        #region OldCode
        //public bool getInqPenjualanBarang(ref DataTable dt, DateTime tanggal1, DateTime tanggal2, out string msg)
        //{
        //    try
        //    {

        //        msg = "";
        //        sSQL = "select * from  vw_trx where substring(kdbarang,1,1)<>'S' ";


        //        if (tanggal1 != tanggal2)
        //        {
        //            sSQL += "and tgltrx between  '" + tanggal1.ToString("yyyy-MM-dd") + "' and '" + tanggal2.ToString("yyyy-MM-dd") + "' ";
        //        }
        //        else
        //        {
        //            sSQL += "and tgltrx ='" + tanggal1.ToString("yyyy-MM-dd") + "'  ";
        //        }


        //        sSQL += "order by tgltrx,trxid";
        //        //dt = Dbserver.ExecuteQuery(sSQL, inqTableName);


        //        if (dt.Rows.Count > 0)
        //        {
        //            msg = dt.Rows.Count.ToString() + " Record(s)";
        //            return true;
        //        }
        //        else
        //        {
        //            msg = "Records Not Found";
        //            return false;
        //        }


        //    }
        //    catch (Exception ex)
        //    {

        //        throw ex;
        //    }

        //}
        //public bool AddNewRow(out string msg1, params object[] fldValue)
        //{
        //    try
        //    {
        //        msg = "";
        //        sSQL = "select * from tb_barang limit 0";
        //        DataTable dt = new DataTable();
        //        dt = Dbserver.ExecuteQuery(sSQL, "tb_barang");
        //        DataRow dr = dt.NewRow();
        //        int i = 0;
        //        int j;
        //        fldValue[0] = new cDBHelper().genIDCommit("GENKDBRG");
        //        foreach (DataColumn dc in dt.Columns)
        //        {
        //            dr[dc.ColumnName] = fldValue[i];
        //            i++;
        //        }
        //        dt.Rows.Add(dr);
        //        if (Dbserver.openConn())
        //        {
        //            j = Dbserver.SqlUpdate(dt, "tb_barang", out msg1);
        //            if (j == 1)
        //            {
        //                msg1 = "Sukses";
        //                Dbserver.CommitAndCloseConn();
        //                return true;
        //            }
        //            else
        //            {
        //                return false;
        //            }
        //        }
        //        else
        //        {
        //            msg1 = "Database unreachable";
        //            return false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        throw ex;
        //    }
        //}
        //public bool EditRowDt(out string msg1, string kdbarang, params object[] fldValue)
        //{
        //    try
        //    {
        //        msg1 = "";
        //        DataTable dt = new DataTable();
        //        // inqTableName = "tb_transstudio";
        //        sSQL = "select * from tb_barang where kdbarang='" + kdbarang + "' ";
        //        dt = Dbserver.ExecuteQuery(sSQL, "tb_barang");
        //        if (dt.Rows.Count > 0)
        //        {
        //            //  inqTableName = "vw_booking";
        //            DataRow dr = dt.Rows[0];
        //            int i = 0;
        //            int j = 0;
        //            foreach (DataColumn dc in dt.Columns)
        //            {
        //                dr[dc.ColumnName] = fldValue[i];
        //                i++;
        //            }
        //            if (Dbserver.openConn())
        //            {

        //                j = Dbserver.SqlUpdate(dt, "tb_barang", out msg1);
        //                if (j == 1)
        //                {
        //                    msg1 = "Records Update Success. BookID=" + kdbarang;
        //                    Dbserver.CommitAndCloseConn();
        //                    return true;
        //                }
        //                else
        //                {
        //                    return false;
        //                }
        //            }
        //            msg1 = "Update Success";
        //            return true;
        //        }
        //        else
        //        {
        //            //    inqTableName = "vw_booking";
        //            msg1 = "Records not Found";
        //            return false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        throw ex;
        //    }
        //} 
        #endregion






    }
}
